package net.sea.setup.ui.wizard.impl.cfg.impl;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import net.sea.commons.constants.AppConstants;
import net.sea.setup.config.custom.CustomConfig;
import net.sea.setup.config.custom.CustomConfigItem;
import net.sea.setup.ui.wizard.IWizardPage.PageResult;
import net.sea.setup.ui.wizard.impl.cfg.AbstractCfgPanel;

/**
 * 
 * 服务器配置页面
 *
 * @author huachengwu 
 */
public class ServerCfgPanel extends AbstractCfgPanel {
	private static final long serialVersionUID = 8900606068567456521L;
	private JTextField serverIPText;
	private final JTextField serverPortText = new JTextField();
	private JTextField graphicalAddressText;
	private JTextField imgAddressText;
	private JTextField logAddressText;

	public ServerCfgPanel() {
		create();
	}

	@Override
	public PageResult validateInput() {
		// 验证IP
		if (!isIP(serverIPText.getText())) {
			return new PageResult(false, "无效的IP地址");
		}
		// 验证端口
		if (!isPort(serverPortText.getText())) {
			return new PageResult(false, "无效的端口号");
		}
		return new PageResult(true);
	}

	/**
	 * 创建页面
	 */
	@Override
	protected void create() {
		setBorder(new TitledBorder(null, "\u670D\u52A1\u5668\u914D\u7F6E",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagLayout gbl_serverCfgPanel = new GridBagLayout();
		gbl_serverCfgPanel.columnWidths = new int[] { 10, 0, 30, 0, 0, 10, 0 };
		gbl_serverCfgPanel.rowHeights = new int[] { 10, 0, 0, 0, 0, 10, 0 };
		gbl_serverCfgPanel.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0,
				1.0, 0.0, Double.MIN_VALUE };
		gbl_serverCfgPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, Double.MIN_VALUE };
		setLayout(gbl_serverCfgPanel);

		JLabel serverIPLb = new JLabel("\u670D\u52A1\u5668\u53D1\u5E03IP\uFF1A");
		serverIPLb.setHorizontalAlignment(SwingConstants.CENTER);
		GridBagConstraints gbc_serverIPLb = new GridBagConstraints();
		gbc_serverIPLb.anchor = GridBagConstraints.EAST;
		gbc_serverIPLb.insets = new Insets(0, 0, 5, 5);
		gbc_serverIPLb.gridx = 1;
		gbc_serverIPLb.gridy = 1;
		add(serverIPLb, gbc_serverIPLb);

		serverIPText = new JTextField();
		GridBagConstraints gbc_serverIPText = new GridBagConstraints();
		gbc_serverIPText.fill = GridBagConstraints.HORIZONTAL;
		gbc_serverIPText.insets = new Insets(0, 0, 5, 5);
		gbc_serverIPText.gridx = 2;
		gbc_serverIPText.gridy = 1;
		add(serverIPText, gbc_serverIPText);
		serverIPText.setText("127.0.0.1");
		serverIPText.setColumns(10);

		JLabel serverPortLb = new JLabel(
				"\u670D\u52A1\u5668\u53D1\u5E03\u7AEF\u53E3\uFF1A");
		GridBagConstraints gbc_serverPortLb = new GridBagConstraints();
		gbc_serverPortLb.anchor = GridBagConstraints.EAST;
		gbc_serverPortLb.insets = new Insets(0, 0, 5, 5);
		gbc_serverPortLb.gridx = 3;
		gbc_serverPortLb.gridy = 1;
		add(serverPortLb, gbc_serverPortLb);
		GridBagConstraints gbc_serverPortText = new GridBagConstraints();
		gbc_serverPortText.fill = GridBagConstraints.HORIZONTAL;
		gbc_serverPortText.insets = new Insets(0, 0, 5, 5);
		gbc_serverPortText.gridx = 4;
		gbc_serverPortText.gridy = 1;
		add(serverPortText, gbc_serverPortText);
		serverPortText.setText("9090");
		serverPortText.setColumns(10);

		JLabel graphicalAddressLb = new JLabel(
				"\u56FE\u5F62\u5B58\u50A8\u5730\u5740\uFF1A");
		GridBagConstraints gbc_graphicalAddressLb = new GridBagConstraints();
		gbc_graphicalAddressLb.anchor = GridBagConstraints.EAST;
		gbc_graphicalAddressLb.insets = new Insets(0, 0, 5, 5);
		gbc_graphicalAddressLb.gridx = 1;
		gbc_graphicalAddressLb.gridy = 2;
		add(graphicalAddressLb, gbc_graphicalAddressLb);

		graphicalAddressText = new JTextField();
		graphicalAddressText.setEditable(false);
		GridBagConstraints gbc_graphicalAddressText = new GridBagConstraints();
		gbc_graphicalAddressText.gridwidth = 2;
		gbc_graphicalAddressText.insets = new Insets(0, 0, 5, 5);
		gbc_graphicalAddressText.fill = GridBagConstraints.HORIZONTAL;
		gbc_graphicalAddressText.gridx = 2;
		gbc_graphicalAddressText.gridy = 2;
		add(graphicalAddressText, gbc_graphicalAddressText);
		graphicalAddressText.setColumns(10);

		JButton graphicalAddressBtn = new JButton("...");
		graphicalAddressBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectFolder(graphicalAddressText);
			}
		});
		GridBagConstraints gbc_graphicalAddressBtn = new GridBagConstraints();
		gbc_graphicalAddressBtn.insets = new Insets(0, 0, 5, 5);
		gbc_graphicalAddressBtn.gridx = 4;
		gbc_graphicalAddressBtn.gridy = 2;
		add(graphicalAddressBtn, gbc_graphicalAddressBtn);

		JLabel imgAddressLb = new JLabel(
				"\u8BC1\u8FF9\u5B58\u50A8\u5730\u5740\uFF1A");
		GridBagConstraints gbc_imgAddressLb = new GridBagConstraints();
		gbc_imgAddressLb.anchor = GridBagConstraints.EAST;
		gbc_imgAddressLb.insets = new Insets(0, 0, 5, 5);
		gbc_imgAddressLb.gridx = 1;
		gbc_imgAddressLb.gridy = 3;
		add(imgAddressLb, gbc_imgAddressLb);

		imgAddressText = new JTextField();
		imgAddressText.setEditable(false);
		GridBagConstraints gbc_imgAddressText = new GridBagConstraints();
		gbc_imgAddressText.gridwidth = 2;
		gbc_imgAddressText.insets = new Insets(0, 0, 5, 5);
		gbc_imgAddressText.fill = GridBagConstraints.HORIZONTAL;
		gbc_imgAddressText.gridx = 2;
		gbc_imgAddressText.gridy = 3;
		add(imgAddressText, gbc_imgAddressText);
		imgAddressText.setColumns(10);

		JButton imgAddressBtn = new JButton("...");
		imgAddressBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectFolder(imgAddressText);
			}
		});
		GridBagConstraints gbc_imgAddressBtn = new GridBagConstraints();
		gbc_imgAddressBtn.insets = new Insets(0, 0, 5, 5);
		gbc_imgAddressBtn.gridx = 4;
		gbc_imgAddressBtn.gridy = 3;
		add(imgAddressBtn, gbc_imgAddressBtn);

		JLabel logAddressLb = new JLabel(
				"\u6267\u884C\u65E5\u5FD7\u5730\u5740\uFF1A");
		logAddressLb.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_logAddressLb = new GridBagConstraints();
		gbc_logAddressLb.anchor = GridBagConstraints.EAST;
		gbc_logAddressLb.insets = new Insets(0, 0, 5, 5);
		gbc_logAddressLb.gridx = 1;
		gbc_logAddressLb.gridy = 4;
		add(logAddressLb, gbc_logAddressLb);

		logAddressText = new JTextField();
		logAddressText.setEditable(false);
		GridBagConstraints gbc_logAddressText = new GridBagConstraints();
		gbc_logAddressText.gridwidth = 2;
		gbc_logAddressText.insets = new Insets(0, 0, 5, 5);
		gbc_logAddressText.fill = GridBagConstraints.HORIZONTAL;
		gbc_logAddressText.gridx = 2;
		gbc_logAddressText.gridy = 4;
		add(logAddressText, gbc_logAddressText);
		logAddressText.setColumns(10);

		JButton logAddressBtn = new JButton("...");
		logAddressBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectFolder(logAddressText);
			}
		});
		GridBagConstraints gbc_logAddressBtn = new GridBagConstraints();
		gbc_logAddressBtn.insets = new Insets(0, 0, 5, 5);
		gbc_logAddressBtn.gridx = 4;
		gbc_logAddressBtn.gridy = 4;
		add(logAddressBtn, gbc_logAddressBtn);

	}

	@SuppressWarnings("unchecked")
	@Override
	public CustomConfig buildCfg() {
		String ipText = serverIPText.getText();
		String portText = serverPortText.getText();
		String graphicalPathText = graphicalAddressText.getText();
		String imgPathText = imgAddressText.getText();
		String logPathText = logAddressText.getText();

		// 构建服务端自定义配置
		CustomConfig config = new CustomConfig();
		config.setType("服务器端配置");

		CustomConfigItem ip = new CustomConfigItem();
		ip.setKey("服务器发布IP");
		ip.setValue(ipText);
		config.addItem(ip);

		CustomConfigItem port = new CustomConfigItem();
		port.setKey("服务器发布端口");
		port.setValue(portText);
		config.addItem(port);

		CustomConfigItem graphicalPath = new CustomConfigItem();
		graphicalPath.setKey("图存储路径");
		graphicalPath.setValue(graphicalPathText);
		config.addItem(graphicalPath);

		CustomConfigItem imgPath = new CustomConfigItem();
		imgPath.setKey("证迹存储路径");
		imgPath.setValue(imgPathText);
		config.addItem(imgPath);

		CustomConfigItem logPath = new CustomConfigItem();
		logPath.setKey("日志存储路径");
		logPath.setValue(logPathText);
		config.addItem(logPath);

		// 保存配置系统到系统上下文
		Map<String, CustomConfig> customCfgMap = (Map<String, CustomConfig>) contextInst
				.getAttribute(AppConstants.CUSTOM_CONFIG_MAP_KEY);
		customCfgMap.put(this.getClass().getName(), config);

		// 应用上下文配置
		String serverAddress = "dubbo://" + ipText + ":" + portText;
		contextInst.setAttribute(AppConstants.SERVER_RELEASE_ADDRESS,
				serverAddress);
		contextInst.setAttribute(AppConstants.SERVER_RELEASE_ADDRESS_PORT,
				portText);
		contextInst.setAttribute(AppConstants.SERVER_CHART_PATH,
				graphicalPathText);
		contextInst.setAttribute(AppConstants.SERVER_IMG_PATH, imgPathText);
		contextInst.setAttribute(AppConstants.SERVER_LOG_PATH, logPathText);
		return config;
	}
}
